<template>
    <!--
          作者：luoyiming
          时间：2019-10-25
          描述：权限-管理员列表-添加管理员
      -->
    <el-dialog width="40%" title="添加版本" :visible.sync="dialogVisible" @close="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false">
      <!--form表单-->
      <el-form size="small" ref="form" :model="form" :rules="formRules" :label-width="formLabelWidth">
        <el-form-item label="版本名称" prop="versionName"><el-input v-model="form.versionName" placeholder="请输入版本名称"></el-input></el-form-item>
        <el-form-item label="版本号" prop="versionCode"><el-input v-model="form.versionCode" placeholder="请输入版本号"></el-input></el-form-item>
        <el-form-item label="系统类型" prop="platform">
            <el-radio-group v-model="form.platform">
                <el-radio label="android">Android</el-radio>
                <el-radio label="ios">IOS</el-radio>
                <el-radio label="win">Windows</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="安装包类型" prop="packageType">
            <el-radio-group v-model="form.packageType">
                <el-radio :label="0">整包更新</el-radio>
                <el-radio :label="1">wgt更新</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="是否发行" prop="issue">
            <el-radio-group v-model="form.issue">
                <el-radio :label="true">是</el-radio>
                <el-radio :label="false">否</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="静默更新" prop="silence">
            <el-radio-group v-model="form.silence">
                <el-radio :label="true">是</el-radio>
                <el-radio :label="false">否</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="强制更新" prop="force">
            <el-radio-group v-model="form.force">
                <el-radio :label="true">是</el-radio>
                <el-radio :label="false">否</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="下载地址" prop="downloadUrl"><el-input v-model="form.downloadUrl" placeholder="请输入下载地址"></el-input></el-form-item>
        <el-form-item :label="item.name" v-for="item in nameList" :key="item.langId">
            <el-input type="textarea" v-model="item.value" :placeholder="'请输入'+item.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :loading="loading">确 定</el-button>
      </div>
    </el-dialog>
  </template>
  
  <script>
  import LangApi from '@/api/lang.js';
  import AppVersionApi from '@/api/appversion.js';
  export default {
    data() {
      return {
        /*左边长度*/
        formLabelWidth: '120px',
        /*是否显示*/
        loading: false,
        /*是否显示*/
        dialogVisible: false,
        /*form表单对象*/
        form: {
            versionName: '',
            versionCode:'',
            platform:'android',
            packageType:0,
            issue:false,
            force:false,
            silence:false,
            force:false,
            downloadUrl:'',
            names:''
        },
        langList:[],
        nameList:[],
        /*form验证*/
        formRules: {
            versionName: [
                {
                required: true,
                message: ' ',
                trigger: 'blur'
                }
            ],
            versionCode: [
                {
                required: true,
                message: ' ',
                trigger: 'blur'
                }
            ],
            downloadUrl:[
            {
                required: true,
                message: ' ',
                trigger: 'blur'
                }
            ]
            
        }
      };
    },
    props: ['open','appversion'],
    watch: {
      open: function(n, o) {
        if (n != o) {
          this.dialogVisible = this.open;
          this.nameList=[]
          this.form=this.appversion
          this.form.names=''
          this.getLangList();
        }
      }
    },
    created() {
        
    },
    methods: {
      getLangList(){
        LangApi.getAll().then(res=>{
            this.langList = res.data;
            for(let i=0;i<this.langList.length;i++){
                var lang = this.langList[i];
                var obj=Object.assign({value:this.appversion[lang.varName]},lang);
                this.nameList.push(obj);
            }
        })
      },
      /*添加*/
      onSubmit() {
        let self = this;
        self.$refs.form.validate(valid => {
          if(valid){
            self.loading = true;
            let params = Object.assign({},self.form);
            var names=[]
            this.nameList.forEach(item=>{
                names.push({key:item.varName,value:item.value})
            })
            params.names=JSON.stringify(names)
            AppVersionApi.update(params, true)
              .then(data => {
                self.loading = false;
                self.$message({
                  message: '恭喜你，更新成功',
                  type: 'success'
                });
                self.dialogFormVisible(true);
              })
              .catch(error => {
                self.loading = false;
              });
          }
        })
      },
  
      /*关闭弹窗*/
      dialogFormVisible(e) {
        if (e) {
          this.$emit('close', {
            type: 'success',
            openDialog: false
          });
        } else {
          this.$emit('close', {
            type: 'error',
            openDialog: false
          });
        }
      }
    }
  };
  </script>
  
  <style></style>
  